package com.kkb.shopcenter.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.shopcenter.api.StoreSeckillService;
import com.kkb.shopcenter.common.dto.StoreSeckillDto;
import com.kkb.shopcenter.common.pojo.StoreSeckill;
import com.kkb.shopcenter.common.utils.OrderUtil;
import com.kkb.shopcenter.server.mappers.StoreSeckillMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class StoreSeckillServiceImpl implements StoreSeckillService {

    @Autowired
    private StoreSeckillMapper storeSeckillMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Map<String, Object> queryAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<StoreSeckill> storeSeckills = storeSeckillMapper.selectAll();
        PageInfo<StoreSeckill> pageInfo = new PageInfo<>(storeSeckills);
        ArrayList<StoreSeckillDto> storeSeckillDtos = new ArrayList<>();
        for (StoreSeckill storeSeckill : pageInfo.getList()) {
            StoreSeckillDto storeSeckillDto = new StoreSeckillDto();
            BeanUtils.copyProperties(storeSeckill,storeSeckillDto);
            storeSeckillDtos.add(storeSeckillDto);
        }
        for (StoreSeckillDto storeSeckillDTO : storeSeckillDtos){
            String statusStr = OrderUtil.checkActivityStatus(storeSeckillDTO.getStartTime(),
                    storeSeckillDTO.getStopTime(), storeSeckillDTO.getStatus());
            storeSeckillDTO.setStatusStr(statusStr);
        }
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("data",storeSeckillDtos);
        map.put("totalElements", pageInfo.getTotal());
        return map;
    }

    @Override
    public int saveOrUpdate(StoreSeckill resources) {
        int result;
        long goodsId = resources.getGoodsId();
        Example example = new Example(StoreSeckill.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);

        int count= storeSeckillMapper.selectCountByExample(example);;
        if(count == 0){
            //增加商品
            result=storeSeckillMapper.insertSelective(resources);
        }else {
            //修改商品
            result=storeSeckillMapper.updateByPrimaryKeySelective(resources);
        }

        //以下更新Redis缓存
        String goodidstr = String.valueOf(resources.getGoodsId());
        String key = "goods-stock_" + goodidstr;
        String value = String.valueOf(resources.getStock());
        redisTemplate.opsForHash().put(key,goodidstr,value);
        redisTemplate.expire(key,resources.getStopTime(), TimeUnit.MINUTES);


        return result;
    }

}
